package com.gdqy.FCS.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Brand;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.ClothingType;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Special;
import com.gdqy.FCS.service.BrandService;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.ClothingTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 
 * @author 张伟键
 *类描述：此类全部完工 等待页面接入
 */
@Controller
@RequestMapping("brand")
public class BrandController {
	@Autowired
	private ClothingTypeService clothingTypeService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private ClothingService clothingService;
	//品牌管理
	@RequestMapping("deal_admin_brand")
	public ModelAndView deal_admin_brand(@RequestParam(value="enterprise_id")int enterprise_id,
			Args args,int pageindex, int pagesize){
		ModelAndView modelAndView = new ModelAndView();
		
		PageHelper.startPage(pageindex, pagesize);
		List<Brand> brands = brandService.selectByEnterprise_id(enterprise_id);
		PageInfo<Brand> page = new PageInfo<Brand>(brands);
		//分页对象
		ListResponse<Brand> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(brands);
		modelAndView.addObject("json", json);
		modelAndView.setViewName("enterprise/brand");
		return modelAndView;
	}
	
	//修改品牌
	@RequestMapping("update_brand")
	public @ResponseBody String update(Brand brand){
		String msg="fail";
		if(brandService.update(brand)>0){
			msg="success";
		}
		return msg;
		
	}
	//增加品牌
	@RequestMapping("add_brand")
	public @ResponseBody Object add_brand(Brand brand){
		int enterprise_id = brand.getEnterprise_id();
		String msg= "fail";
		brand.setStatus(1);
		int data = brandService.insert(brand);
		
		ListResponse<Brand> json = new ListResponse<>();
		if(data>0){
			msg="success";
			List<Brand> brands = brandService.selectByEnterprise_id(enterprise_id);
			 json= FastResponse._().errCode(0).subCode(0).errMsg(msg)
					.list(brands);
		}
		return json;
	}
	//删除品牌
	@RequestMapping("delete_brand")
	public @ResponseBody String delete_brand(int brand_id,HttpSession session){
		String msg = "fail";
		Brand brand = brandService.selectById(brand_id);
		//获取session中的供应商id
		int enterprise_id = (int) session.getAttribute("enterprise_id");
		if(enterprise_id == brand.getEnterprise_id()){
			int data = brandService.deleteById(brand_id);
			if(data>0){
				msg="success";
			}
		}
		
		return msg;
	}
	//品牌详情--品牌里面的数据
	@RequestMapping("brand_detail")
	public ModelAndView brand_detail(int brand_id,Args args,int pageindex, int pagesize){
		ModelAndView modelAndView = new ModelAndView();
		PageHelper.startPage(pageindex, pagesize);
		List<Clothing> clothings = clothingService.selectByBrand_id(brand_id);
		PageInfo<Clothing> page = new PageInfo<Clothing>(clothings);
		//分页对象
		ListResponse<Clothing> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
		.args(args.getReqeustargs()).pageIndex(page.getPageNum())
		.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
		.hasNext(page.isHasNextPage()).list(clothings);
		modelAndView.addObject("json", json);
		
		modelAndView.setViewName("enterprise/brand_detail");
		return modelAndView;
	}
	
	//获取服装分类与品牌
	@RequestMapping("getClothing_class")
	public @ResponseBody Map<String, Object> getClothing_class(@RequestParam(value = "parent_code",required = true ) String parent_code,
			String code){
		List<ClothingType> clothingTypes = clothingTypeService.selectClothing_class(parent_code);
		List<Brand> brands = brandService.getBrandByClothing(code);  //修改处
		Map<String, Object> type_and_brand = new HashMap<>();
		type_and_brand.put("clothingTypes", clothingTypes);
		type_and_brand.put("brands", brands);
		
		return type_and_brand;	
	}
	
	//感觉服装小分类获取服装品牌    修改处
		@RequestMapping("getBrandBySmallCode")
		public @ResponseBody List<Brand> getBrandBySmallCode(@RequestParam(value = "code",required = true ) String code){
			List<Brand> brands = brandService.getBrandByClothing(code);
			return brands;	
		}
}
